<template>
  <UView name="数据采集任务">
    <UViewSearch v-model="searchModel">
      <URow col="search">
        <UInput field="name" label="任务名称" :allowClear="true"/>
        <UInput field="status" label="任务状态" :allowClear="true"/>
        <UFuncBtn func="query" url="/iot/collectTask/view">搜索</UFuncBtn>
        <UFuncBtn func="reset">重置</UFuncBtn>
        <UFuncBtn func="add" url="/iot/collectTask/add" v-auth="'iot:collectTask:add'">新增</UFuncBtn>
      </URow>
    </UViewSearch>
    <UViewModal title="数据采集任务管理" :span="[7, 14]" v-model="editModel" :rules="rules">
      <template #default="{model}">
        <UInput field="name" label="任务名称" />
        <UInput field="cron" label="调度" />
        <UTreeSelect field="typeId" label="设备类型" labelField="name"
             url="/iot/deviceType/tree" valueField="id" />
        <USelect field="modelId" label="设备型号" labelField="model" valueField="id"
                 url="/iot/deviceModel/listByType?typeId={typeId}" />
        <USelect field="uid" label="采集设备" labelField="name" valueField="uid"
                 url="/iot/device/listByModelId?modelId={modelId}"  />
        <UInput field="remark" label="任务说明" />
      </template>
      <template #footer>
        <UFuncBtn func="cancel">取消</UFuncBtn>
        <UFuncBtn func="submit" url="/iot/collectTask/saveOrUpdate">提交</UFuncBtn>
        <UFuncBtn func="reset">重置</UFuncBtn>
      </template>
    </UViewModal>
    <UViewTable :columns="columns">
      <template #status="{record, value}">
        <span>
        <ABadge v-if="record.status=='run'" status="processing"/>
        <ABadge v-else status="default"/>
        <UFuncTag func="ajax:confirm" url="/iot/collectTask/status" :data="record" :params="getStatusParams"
                  :config="{confirmTitle: '采集任务切换', confirmContent: `采集状态是否要从[${value}]切换到[${record.status=='stop' ? '运行' : '停止'}]?`}">
          {{value}}
        </UFuncTag>
        </span>
      </template>
      <template #action="{record}">
        <UFuncTag func="edit" :data="record" url="/iot/collectTask/edit" v-auth="'iot:collectTask:edit'">修改</UFuncTag>
        <UFuncTag func="del" :data="record" url="/iot/collectTask/del" v-auth="'iot:collectTask:del'">删除</UFuncTag>
      </template>
    </UViewTable>
  </UView>
</template>
<!-- 数据采集任务功能 -->
<script>
import {reactive, ref} from "vue";

export default {
  name: "CollectTask",
  setup() {
    let columns = ref([
      {field: "name", title: "任务名称"},
      {field: "cron", title: "任务调度"},
      {field: "modelName", title: "设备型号"},
      {field: "deviceName", title: "采集设备"},
      {field: "remark", title: "采集任务说明"},
      {field: "createTime", title: "创建时间", type: 'date', format: 'YYYY-MM-DD'},
      {field: "updateTime", title: "更新时间", type: 'date', format: 'YYYY-MM-DD'},
      {field: "status", title: "任务状态", dict: 'iot_collect_status'},
      {field: 'action', title: '操作', type: 'action'},
    ]);

    let rules = reactive({
      name: {required: true, message: '任务名称必填'},
      cron: {required: true, message: '任务调度必填'},
      modelId: {required: true, message: '设备型号必填'},
    })

    let editModel = ref({});
    let searchModel = ref({});
    return {columns, rules, editModel, searchModel}
  },
  methods: {
    getStatusParams(record) {
      return Promise.resolve({id: record.id, status: record.status == 'run' ? 'stop' : 'run'})
    }
  }
}
</script>
<style scoped> </style>